package com.ny.controller;
import com.ny.entity.Result;
import com.ny.entity.User;
import com.ny.service.impl.UserService;
import com.ny.until.PasswordUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController

public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/selectAllUser")
    public Result selectAllUser() {
        return Result.success(userService.findAllUser());
    }

    // 登录:http://localhost:8081/login?username=admin&password=<PASSWORD>
// 修改 UserController 的 login 方法
    @PostMapping("/login")
    public Result findUser(String username, String password) {
        User user = userService.findUserByUsername(username);
        if (user == null) {
            return Result.notFound("用户不存在");
        }
        // 临时注释加密，直接用明文对比（测试用）
        // String encryptedPassword = PasswordUntil.encryptPassword(password);
        // if (!encryptedPassword.equals(user.getPassword())) {
        if (!password.equals(user.getPassword())) { // 直接明文对比
            return Result.error("密码错误");
        }
        return Result.success(user);
    }

    //    根据用户名查询用户信息
//    http://localhost:8081/findUserByUsername?username=admin
    @GetMapping("/findUserByUsername")
    public Result findUserByUsername(String username) {
        User user = userService.findUserByUsername(username);
        System.out.println(user);
        if (username != null && user != null) {
            return Result.success(user);
        }
        return Result.notFound("用户名不存在");
    }

    // 注册
    // UserController 的 registerUser 方法
    // 确保注册时加密（UserController）
    @PostMapping("/registerUser")
    public Result registerUser(User user) {
        // 调用同一个加密方法
        user.setPassword(PasswordUntil.encryptPassword(user.getPassword()));
        return Result.success(userService.registerUser(user));
    }

    @GetMapping("/findUserByEmail")
    public Result findUserByEmail(String email) {
        User user = userService.findUserByEmail(email);
        if (user != null) {
            return Result.success(user);
        }
        return Result.notFound("邮箱号不存在");
    }

    @GetMapping("/sendEmailCode")
    public Result sendEmailCode(String email){
        User user = userService.findUserByEmail(email);
        if (user!= null){
            userService.sendEmailCode(email);
            return Result.success("发送成功");
        }
        return Result.notFound("邮箱号不存在");
    }

    @PutMapping("/updatePasswordByEmail")
    public Result updatePasswordByEmail(String email, String password,String code){
        Boolean checkCode =userService.checkCode(email, code);
        if (checkCode){
            int i =userService.updatePasswordByEmail(email, password);
            if (i>0){
                return Result.success("修改成功");
            }
            return Result.notFound("成功获取验证码");
        }
        else {
            return Result.success("验证码错误");
        }

    }
}